/**
 * Created by tanbingqin on 2020/8/18.
 */
import {messages} from "share/common";
/**
 * 操作：去借款按钮
 * 适用：行程卡片的差旅申请单
 */
import React from 'react'
import { connect } from 'react-redux'
import menuRoute from 'routes/menuRoute'
import { Form, Button, message, Modal, Row, Col, Switch, Select } from 'antd'
const Option = Select.Option;
import travelService from 'containers/request/travel-request/travel.service'
import requestService from 'containers/request/request.service'
import errorMessage from 'share/errorMessage';
import requestUtil from 'containers/request/slide/request-util'
import Condition from 'components/condition';
const If = Condition.If;

class GoLoanBtn extends React.Component{
  constructor(props) {
    super(props);
    this.state = {
      loading: false,
    };
    this.newLoan = menuRoute.getRouteItem('new-borrow','key'); //新建单行、多行借款单
  }

  goAutoLoan = () => {
    const { info, form: formInfo, jobInfo } = this.context;
    const { loginUser } = this.props;
    //1.判断预算明细个人支付金额是否大于0
    if (info.personalPaymentAmount > 0) {
      //2.校验是否有权限创建借款单
      let loanFormOID = '';
      if (formInfo.customFormPropertyMap && formInfo.customFormPropertyMap["application.direct.loan.type"]) {
        loanFormOID = formInfo.customFormPropertyMap["application.direct.loan.type"];
      }
      let params = {
        applicantOID: info.applicantOID,
        loanFormOID,
        jobId: jobInfo.id,
        applicationFormOID: formInfo.formOID
      };
      this.setState({loading: true});
      requestService.checkAutoLoan(params).then(res => {
        this.setState({loading: false});
        let url = '';
        url = this.newLoan.url.replace(':formOID', loanFormOID).replace(':applicationOID', info.applicationOID).replace(':jobId', jobInfo.id);
        if (info.applicantOID !== loginUser.userOID) {
          url = url.replace(':applicantOID', info.applicantOID);
        }
        this.context.router.push(url);
      }).catch(err => {
        errorMessage(err.response);
        this.setState({loading: false});
      });
    } else {
      message.error(messages('request-1.key263')/*个人支付金额需大于0*/);
    }
  };

  render() {
    const { loading } = this.state;
    const { info, form: formInfo } = this.context;
    return (
      <div className="request-btn">
        {requestUtil.isShowAutoLoanBtn(info, formInfo.customFormPropertyMap) && (
          <Button type="primary" onClick={this.goAutoLoan} loading={loading}>{messages('request-1.key264')/*去借款*/}</Button>
        )}
      </div>
    )
  }
}

GoLoanBtn.contextTypes = {
  applicant: React.PropTypes.object,
  creditScore: React.PropTypes.object,
  jobInfo: React.PropTypes.object,
  form: React.PropTypes.object,
  info: React.PropTypes.object,
  close: React.PropTypes.func,
  refreshInfo: React.PropTypes.func,
  router: React.PropTypes.object,
};

function mapStateToProps(state) {
  return {
    loginUser: state.login.loginUser
  }
}

export default connect(mapStateToProps)(GoLoanBtn)